# -*- coding: utf-8 -*-

from PyQt4.QtGui import QWidget
from PyQt4.QtGui import QPushButton
from PyQt4.QtGui import QLineEdit
from PyQt4.QtGui import QHBoxLayout
from PyQt4.QtGui import QIcon
from PyQt4.QtCore import SIGNAL
from PyQt4.QtCore import QSettings
from PyQt4.QtCore import QString

from ninja_ide import resources


class RecentProjectItem(QWidget):

    def __init__(self, project, content, itemRelated, parent=None):
        QWidget.__init__(self, parent)
        self.content = content.toMap()
        self.project = project
        self.favorite = QPushButton(self)
        self.delete = QPushButton(self)
        self.delete.setIcon(QIcon(resources.IMAGES['delProj']))
        self.name = QLineEdit(self)
        self.itemRelated = itemRelated
        self.setStyleSheet(
            "QPushButton, QLineEdit{background:white; border:none}")
        self.name.setText(self.content[QString("name")].toString())
        self.delete.setToolTip(self.tr("Click to delete from the list"))
        self.setLayout(QHBoxLayout())
        self.layout().setContentsMargins(0, 0, 0, 0)
        self.layout().addWidget(self.favorite)
        self.layout().addWidget(self.name)
        self.layout().addWidget(self.delete)
        self.connect(self.favorite, SIGNAL("clicked(bool)"),
            self.onClickOnFavorite)
        self.connect(self.delete, SIGNAL("clicked(bool)"),
            self.onClickOnDelete)
        #TODO: Change this click listen it doesn't work with ReadOnly = True
        self.connect(self.name,
            SIGNAL("cursorPositionChanged(int, int)"), self.onClickOnName)
        self.setFavorite(self.content[QString("isFavorite")].toBool())

    def onClickOnDelete(self):
        settings = QSettings()
        recent_projects = settings.value("recentProjects").toMap()
        if self.project in recent_projects:
            del recent_projects[self.project]
            settings.setValue("recentProjects", recent_projects)
            self.emit(SIGNAL("deleteMe(QListWidgetItem)"), self.itemRelated)

    def onClickOnFavorite(self):
        settings = QSettings()
        recent_projects = settings.value("recentProjects").toMap()
        properties = recent_projects[self.project].toMap()
        properties[QString("isFavorite")] = not properties[
            QString("isFavorite")].toBool()
        recent_projects[self.project] = properties
        settings.setValue("recentProjects", recent_projects)
        self.emit(SIGNAL("favoriteChange(bool)"), properties[
            QString("isFavorite")])
        self.setFavorite(properties[QString("isFavorite")])

    def onClickOnName(self, uno, dos):
        self.emit(SIGNAL("clicked(QString)"), self.project)

    def setFavorite(self, isFavorite):
        if isFavorite:
            self.favorite.setIcon(QIcon(resources.IMAGES['favProj']))
            self.favorite.setToolTip(
                self.tr("Click to remove from favorite projects"))
        else:
            self.favorite.setIcon(QIcon(resources.IMAGES['unfavProj']))
            self.favorite.setToolTip(
                self.tr("Click to add to favorite projects"))

    def getProjectPath():
        return self.project

    def getItem():
        return self.itemRelated

